package cn.shenlong.portal.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.shenlong.common.Constants;
import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Discount;
import cn.shenlong.portal.service.DiscountService;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/discount/admin")
public class DiscountAController {

	@Autowired
	private DiscountService discountService;
	
	/**
	 * 商家优惠信息列表
	 * @param searchProperty 搜索参数名
	 * @param searchValue    搜索值
	 * @param pageNo
	 * @param pageSize
	 * @param storeId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(String searchProperty, String searchValue,Integer pageNumber, Integer pageSize,Long storeId, String resultStr,
			HttpServletRequest request, ModelMap model) {
		if (pageNumber == null) {
			pageNumber = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		Map<String, Object> para = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(searchValue)) {
            model.addAttribute("searchProperty", searchProperty);
            model.addAttribute("searchValue", searchValue);
            if(StringUtils.isNotBlank(searchProperty)){
            	if(searchProperty.equals("title")){
            		para.put("title",searchValue);
            	}
            }
        }		
        para.put("storeId",storeId);
        Page<Discount> p = discountService.findPageListByCriteria(pageNumber, pageSize,para);
		model.addAttribute("page", p);
		model.addAttribute("storeId", storeId);
        if (StringUtils.isNotBlank(resultStr)) {
            try {
                resultStr = URLDecoder.decode(resultStr, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        model.addAttribute("resultStr", resultStr);
		return "/server/store/discount/list";
	}
	
	/**
	 * 跳转新增界面
	 * @param storeId
	 * @param model
	 * @return
	 */
	@RequestMapping("/add")
	public String add(Long storeId,ModelMap model){
		model.addAttribute("storeId", storeId);
		return "/server/store/discount/add";
	}
	
	/**
	 *  保存信息
	 * @param discount
	 * @param model
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(Discount discount,ModelMap model){
        discountService.saveEntity(discount);
        String resultStr = "保存成功！";
        try {
            model.addAttribute("resultStr", URLEncoder.encode(resultStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
		model.addAttribute("storeId", discount.getStoreId());
        return new ModelAndView("redirect:list.html", model);
	}
	
	/**
	 * 跳转编辑界面
	 * @param discountId
	 * @param model
	 * @return
	 */
	@RequestMapping("/edit")
	public String edit(Long discountId,ModelMap model){
		Discount discount=discountService.findByPK(discountId);
		model.addAttribute("discount", discount);
		model.addAttribute("discountId", discountId);
		return "/server/store/discount/edit";
	}	
	
	/**
	 * 跳转查看界面
	 * @param discountId
	 * @param model
	 * @return
	 */
	@RequestMapping("/show")
	public String show(Long discountId,ModelMap model){
		Discount discount=discountService.findByPK(discountId);
		model.addAttribute("discount", discount);
		return "/server/store/discount/show";
	}		
	
	/**
	 *  更新信息
	 * @param discount
	 * @param model
	 * @return
	 */
	@RequestMapping("/update")
	public ModelAndView update(Discount discount,ModelMap model){
        discountService.updateEntity(discount);
        String resultStr = "保存成功！";
        try {
            model.addAttribute("resultStr", URLEncoder.encode(resultStr, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
		model.addAttribute("storeId", discount.getStoreId());
        return new ModelAndView("redirect:list.html", model);
	}	
	
	/**
	 * 删除商家优惠信息
	 * @param ids
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete( String ids,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (ids != null) {
				String[] tmp = ids.split(",");
				Integer[] discountIds = new Integer[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					discountIds[i] = Integer.parseInt(tmp[i]);
				}
				discountService.deleteByPKArray(discountIds);
			}  
			resultMap.put("result", "success");
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", "删除失败！原因：" + e.toString());
		}
		return resultMap;
	}	
	
	/**
	 * ajax上传图片
	 */
	@RequestMapping("/uploadImage")
    @ResponseBody
	public Map<String,Object> uploadImage(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		try{
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(request,Constants.DISCOUNT_IMG_URI);	
			resultMap.put("path", resultList.get(0).get("imagePath"));
			resultMap.put("result", true);
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", false);
		}		
		return resultMap;
	}	
}
